package com.foreveross.proxyip.monitor.domain;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.foreveross.proxyip.entity.RetrieveIpEntity;
import com.foreveross.proxyip.infra.InstanceFactory;
import com.foreveross.proxyip.infra.utils.StringUtils;
import com.foreveross.proxyip.monitor.infra.RealTimeSearchRepository;
import com.foreveross.proxyip.monitor.vo.RetrieveIpEntityVO;

/**
 * 实时检索领域对象
 * @author Administrator
 *
 */
public class RealTimeSearch {

	public final static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	private static RealTimeSearchRepository repository;
	
	private static RealTimeSearchRepository getRepository(){
		if(repository==null){
			repository=InstanceFactory.getInstance(RealTimeSearchRepository.class);
		}
		return repository;
	}
	
	/**
	 * 检索，根据某些条件
	 * @param vo
	 * @return
	 */
	public static List<RetrieveIpEntity> searchByConditions(RetrieveIpEntityVO vo,int pageIndex,int pageLen)throws Exception{
		
		
		StringBuffer hql=new StringBuffer("select r from RetrieveIpEntity r where 1=1");
		List<Object> params=new ArrayList<Object>();
		Object[] obj=generateHqlByConditions(hql, params, vo);
		hql=(StringBuffer)obj[0];
		params=(List<Object>)obj[1];
		//检索时间
		hql.append(" order by r.retrieveTime desc");
		
		return getRepository().findByHQL(hql.toString(), params.toArray(),pageIndex,pageLen);
	}
	
	/**
	 * 根据某些条件获取到所有的符合的数量
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	public static long searchSizeByConditions(RetrieveIpEntityVO vo)throws Exception{
		StringBuffer hql=new StringBuffer("select count(r) from RetrieveIpEntity r where 1=1");
		List<Object> params=new ArrayList<Object>();
		Object[] obj=generateHqlByConditions(hql, params, vo);
		hql=(StringBuffer)obj[0];
		params=(List<Object>)obj[1];
		//检索时间倒叙
		//hql.append(" order by r.retrieveTime desc");
		
		return getRepository().findSizeByHQL(hql.toString(), params.toArray());
	}
	
	
	
	
	
	
	/**
	 * 根据条件拼出通用的查询语句和拼出条件参数
	 * [0]==HQL
	 * [1]==params
	 */
	private static Object[] generateHqlByConditions(StringBuffer hql,List<Object> params,RetrieveIpEntityVO vo){
		Object[] finalResult=new Object[2];
		if(vo!=null){
			
		    if(!StringUtils.isEmpty(vo.getId())){
					hql.append(" and r.id=?");
					params.add(vo.getId());
			}
			if(!StringUtils.isEmpty(vo.getArea())){
				hql.append(" and r.area=?");
				params.add(vo.getArea());
			}
	//		if(vo.getCreateTimeStr()!=null){
	//			hql.append(" and to_char('r.r.createTime','yyyy-MM-dd hh:mm:ss')=?");
	//			params.add(vo.getCreateTimeStr());
	//		}
			if(!StringUtils.isEmpty(vo.getIp())){//IP字段是模糊查询的
				hql.append(" and r.ip like ?");
				params.add("%"+vo.getIp()+"%");
			}
			if(!StringUtils.isEmpty(vo.getPortStr())){//端口字段是固定查询
				hql.append(" and cast(r.port as string)=?");
				params.add(vo.getPortStr());
			}
	//		if(vo.getRetrieveTimeStr()!=null){
	//			hql.append(" and to_char('r.retrieveTime','yyyy-MM-dd hh:mm:ss')=?");
	//			params.add(vo.getCreateTimeStr());
	//		}
	//		if(vo.getStateStr()!=null){
	//			hql.append(" and cast(r.state as string)=?");
	//			params.add(vo.getStateStr());
	//		}
			if(!StringUtils.isEmpty(vo.getRetrieveChannel())){//渠道是指定查询的
				hql.append(" and r.retrieveChannel=?");
				params.add(vo.getRetrieveChannel());
			}
			if(!StringUtils.isEmpty(vo.getBeforeSearchMinuteStr())){//检索时间，在XXX分钟之内
				//就是当前时间到当前时间减去XXX分钟这段时间
				Date end=new Date();
				
				Integer minute=Integer.parseInt(vo.getBeforeSearchMinuteStr());
				long time=end.getTime()-minute*60*1000;
				Date start=new Date(time);
//				hql.append(" and r.retrieveTime between cast(? as DATE) and " +
//						"cast(? as DATE)");
				hql.append(" and r.retrieveTime between ? and ?");
				params.add(start);
				params.add(end);
			}
		}
		finalResult[0]=hql;
		finalResult[1]=params;
		return finalResult;
	}
	
	
	
	
	
	
}
